package org.lvhengchang.schoolmanager.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lvhengchang.schoolmanager.bean.Teacher;
import org.lvhengchang.schoolmanager.service.TeacherService;
import org.lvhengchang.schoolmanager.service.impl.TeacherServiceImpl;

/**
 * 新增教师请求处理类
 * @author LvHengchang
 *
 */
@WebServlet("/teacher/add")
public class TeacherAddServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
    private TeacherService TeacherService = new TeacherServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    	
    	req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
          
    	String teacher_name = (String) req.getParameter("teacher_name");
        String teacher_birthday = (String) req.getParameter("teacher_birthday");
        String sex = (String) req.getParameter("sex");
        String remarks = (String) req.getParameter("remarks");

        Teacher teacher = new Teacher();
        teacher.setTeacher_name(teacher_name);
        teacher.setTeacher_birthday(teacher_birthday);
        teacher.setSex(sex);
        teacher.setRemarks(remarks);

        try {
            TeacherService.saveTeacher(teacher);
            req.setAttribute("teacher_name", teacher_name);
            req.getRequestDispatcher("/teacher/teacher_add_success.jsp").forward(req, resp);
        } catch (Exception e) {
            throw new ServletException(e);
        }

    }

}
